<?php
/**
*
* @package ucp
* @version $Id: ucp_profile.php 10060 2009-08-28 09:26:43Z nickvergessen $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* ucp_profile
* Changing profile settings
*
* @todo what about pertaining user_xspace_options?
* @package ucp
*/
class ucp_xspace
{
	var $u_action;

	function main($id, $mode)
	{
		global $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx;

		$user->add_lang('posting');

		$preview	= (!empty($_POST['preview'])) ? true : false;
		$submit		= (!empty($_POST['submit'])) ? true : false;
		$delete		= (!empty($_POST['delete'])) ? true : false;
		$error = $data = array();
		$s_hidden_fields = '';

		switch ($mode)
		{
			case 'categories':
				$form_key = 'ucp_xspace_categories';
				add_form_key($form_key);

				$action		= request_var('action', '');
				$category_id	= request_var('c', 0);
				$parent_id = request_var('parent_id', 0);
				$update		= (isset($_POST['update'])) ? true : false;

				$category_data = $errors = array();
				if ($update && !check_form_key($form_key))
				{
					$update = false;
					$errors[] = $user->lang['FORM_INVALID'];
				}

				// Major routines
				if ($update)
				{
					switch ($action)
					{
						case 'delete':
							$action_subcategories	= request_var('action_subcategories', '');
							$subcategories_to_id	= request_var('subcategories_to_id', 0);
							$action_posts		= request_var('action_posts', '');
							$posts_to_id		= request_var('posts_to_id', 0);
							
							delete_category($category_id, $action_posts, $action_subcategories, $posts_to_id, $subcategories_to_id);
							//$cache->destroy('sql', FORUMS_TABLE);
							
							meta_refresh(3, $this->u_action);
							$message = $user->lang['CATEGORY_DELETED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
							trigger_error($message);
						break;
		
						case 'edit':
							$category_data = array(
								'category_id'		=>	$category_id
							);
						// No break here
						case 'add':
							$category_data += array(
								'parent_id'				=> request_var('category_parent_id', $parent_id),
								'category_type'			=> request_var('category_type', CATEGORY_CATEGORY),
								'category_name'			=> utf8_normalize_nfc(request_var('category_name', '', true)),
								'category_desc'			=> utf8_normalize_nfc(request_var('category_desc', '', true)),
								'category_desc_uid'		=> '',
								'category_desc_options'	=> 7,
								'category_desc_bitfield'	=> '',
								'creator_id'		=> $user->data['user_id'],
								'creator_name'			=> $user->data['username'],
								'is_public'			=> 0,
							);

							// Get data for forum description if specified
							if ($category_data['category_desc'])
							{
								generate_text_for_storage($category_data['category_desc'], $category_data['category_desc_uid'], $category_data['category_desc_bitfield'], $category_data['category_desc_options'], request_var('disable_bbcode', true), request_var('disable_magic_url', true), request_var('disable_smilies', true));
							}

							$errors = update_category_data($category_data);
		
							if (!sizeof($errors))
							{
								meta_refresh(3, $this->u_action. '&amp;parent_id=' . $parent_id);
								$message = $user->lang['XSPACE_UPDATED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action. '&amp;parent_id=' . $parent_id . '">', '</a>');
								trigger_error($message);
							}
						break;
					}
				}

				switch ($action)
				{
					case 'add':
					case 'edit':
						// Show category to create/modify a category
						if ($action == 'edit')
						{
							$row = get_category_info($category_id);
							$old_category_type = $row['category_type'];

							if (!$update)
							{
								$category_data = $row;
							}
							else
							{
								$category_data['left_id'] = $row['left_id'];
								$category_data['right_id'] = $row['right_id'];
							}

							// Make sure no direct child forums are able to be selected as parents.
							$exclude_categories = array();
							foreach (get_category_branch($category_id, 'children') as $row)
							{
								$exclude_categories[] = $row['category_id'];
							}

							$parents_list = make_category_select($category_data['parent_id'], $exclude_categories, false);
						}
						else
						{
							$category_id = $parent_id;
							$parents_list = make_category_select($parent_id, false, false);

							// Fill category data with default values
							if (!$update)
							{
								$category_data = array(
									'parent_id'				=> $parent_id,
									'category_type'			=> CATEGORY_CATEGORY,
									'category_name'			=> utf8_normalize_nfc(request_var('category_name', '', true)),
									'category_desc'			=> '',
								);
							}
						}

						$category_desc_data = array(
							'text'			=> $category_data['category_desc'],
							'allow_bbcode'	=> true,
							'allow_smilies'	=> true,
							'allow_urls'	=> true
						);

						// Parse desciption if specified
						if ($category_data['category_desc'])
						{
							if (!isset($category_data['category_desc_uid']))
							{
								// Before we are able to display the preview and plane text, we need to parse our request_var()'d value...
								$category_data['category_desc_uid'] = '';
								$category_data['category_desc_bitfield'] = '';
								$category_data['category_desc_options'] = 0;

								generate_text_for_storage($category_data['category_desc'], $category_data['category_desc_uid'], $category_data['category_desc_bitfield'], $category_data['category_desc_options'], request_var('desc_allow_bbcode', false), request_var('desc_allow_urls', false), request_var('desc_allow_smilies', false));
							}
		
							// decode...
							$category_desc_data = generate_text_for_edit($category_data['category_desc'], $category_data['category_desc_uid'], $category_data['category_desc_options']);
						}

						$template->assign_vars(array(
							'S_EDIT_CATEGORY'		=> true,
							'S_ERROR'			=> (sizeof($errors)) ? true : false,
							'S_PARENT_ID'		=> $parent_id,
							'S_CATEGORY_PARENT_ID'	=> $category_data['parent_id'],
							'S_ADD_ACTION'		=> ($action == 'add') ? true : false,
		
							'U_EDIT_ACTION'	=> $this->u_action . "&amp;parent_id={$parent_id}&amp;action=$action&amp;c=$category_id",
		
							'ERROR_MSG'						=> (sizeof($errors)) ? implode('<br />', $errors) : '',
		
							'CATEGORY_NAME'				=> $category_data['category_name'],
							//'TOPICS_PER_PAGE'			=> $category_data['forum_topics_per_page'],
							'S_CATEGORY_PASSWORD_SET'		=> (empty($category_data['forum_password'])) ? false : true,
		
							'CATEGORY_DESC'				=> $category_desc_data['text'],
							'S_BBCODE_ALLOWED'		=> $config['allow_sig_bbcode'],
							'S_SMILIES_ALLOWED'		=> $config['allow_sig_smilies'],
							'S_LINKS_ALLOWED'		=> ($config['allow_sig_links']) ? true : false,
							'S_BBCODE_CHECKED'		=> ($category_desc_data['allow_bbcode']) ? '' : ' checked="checked"',
							'S_SMILIES_CHECKED'	=> ($category_desc_data['allow_smilies']) ? '' : ' checked="checked"',
							'S_MAGIC_URL_CHECKED'		=> ($category_desc_data['allow_urls']) ? '' : ' checked="checked"',
		
							//'S_CATEGORY_TYPE_OPTIONS'		=> $category_type_options,
							'S_PARENT_OPTIONS'			=> $parents_list,
							//'S_STYLES_OPTIONS'			=> $styles_list,
						));

					break;

					case 'delete':
		
						if (!$category_id)
						{
							trigger_error($user->lang['NO_CATEGORY'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action. '&amp;parent_id=' . $parent_id . '">', '</a>'), E_USER_WARNING);
						}

						$category_data = get_category_info($category_id);
		
						$subcategories_id = array();
						$subcateories = get_category_branch($category_id, 'children');
		
						foreach ($subcateories as $row)
						{
							$subcategories_id[] = $row['category_id'];
						}

						$categories_list = make_category_select($category_data['parent_id'], $subcategories_id);

						$sql = 'SELECT category_id
							FROM ' . XSPACE_CATEGORIES_TABLE . '
							WHERE category_type = ' . CATEGORY_CATEGORY . "
								AND category_id <> $category_id";
						$result = $db->sql_query_limit($sql, 1);

						if ($db->sql_fetchrow($result))
						{
							$template->assign_vars(array(
								'S_MOVE_CATEGORY_OPTIONS'		=> make_category_select($category_data['parent_id'], $subcategories_id, false, false)) 
							);
						}
						$db->sql_freeresult($result);

						$parent_id = ($parent_id == $category_id) ? 0 : $parent_id;

						$template->assign_vars(array(
							'S_DELETE_CATEGORY'		=> true,
							'U_ACTION'				=> $this->u_action . "&amp;parent_id={$parent_id}&amp;action=delete&amp;c=$category_id",
							'U_BACK'				=> $this->u_action . '&amp;parent_id=' . $parent_id,

							'CATEGORY_NAME'			=> $category_data['category_name'],
							'S_HAS_SUBCATEGORIES'		=> ($category_data['right_id'] - $category_data['left_id'] > 1) ? true : false,
							'S_CATEGORIES_LIST'			=> $categories_list,
							'S_ERROR'				=> (sizeof($errors)) ? true : false,
							'ERROR_MSG'				=> (sizeof($errors)) ? implode('<br />', $errors) : '')
						);

					break;

					case 'sync':
					case 'move_up':
					case 'move_down':

						if (!$category_id)
						{
							trigger_error($user->lang['NO_CATEGORY'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action. '&amp;parent_id=' . $parent_id . '">', '</a>'), E_USER_WARNING);
						}
						
						$sql = 'SELECT *
							FROM ' . XSPACE_CATEGORIES_TABLE . "
							WHERE category_id = $category_id";
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						$db->sql_freeresult($result);

						if (!$row)
						{
							trigger_error($user->lang['NO_CATEGORY'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action. '&amp;parent_id=' . $parent_id . '">', '</a>'), E_USER_WARNING);
						}

						if ($action == 'move_up' || $action == 'move_down') 
						{
							move_category_by($row, $action, 1);
						} 
						else 
						{
							@set_time_limit(0);
							sync_category($category_id);
						}

					default:
						// Default management page
						if (!$parent_id)
						{
							$navigation = $user->lang['CATEGORY_INDEX'];
						}
						else
						{
							$navigation = '<a href="' . $this->u_action . '">' . $user->lang['CATEGORY_INDEX'] . '</a>';

							$categories_nav = get_category_branch($parent_id, 'parents', 'descending');
							foreach ($categories_nav as $row)
							{
								if ($row['category_id'] == $parent_id)
								{
									$navigation .= ' -&gt; ' . $row['category_name'];
								}
								else
								{
									$navigation .= ' -&gt; <a href="' . $this->u_action . '&amp;parent_id=' . $row['category_id'] . '">' . $row['category_name'] . '</a>';
								}
							}
						}

						// Jumpbox
						$category_box = make_category_select($parent_id, false, false, false, false); //make_category_select($parent_id);

						if ($action == 'sync')
						{
							$template->assign_var('S_RESYNCED', true);
						}

						$sql = 'SELECT *
							FROM ' . XSPACE_CATEGORIES_TABLE . ' 
							WHERE category_type = ' . CATEGORY_CATEGORY . ' AND parent_id = ' . $parent_id . ' AND creator_id = ' . $user->data['user_id'] . '
							ORDER BY left_id';
						$result = $db->sql_query($sql);

						if ($row = $db->sql_fetchrow($result))
						{
							do {
								$category_type = $row['category_type'];

								$url = $this->u_action . "&amp;parent_id=$parent_id&amp;c={$row['category_id']}";

								$template->assign_block_vars('categories', array(
									//'FOLDER_IMAGE'		=> $folder_image,
									'CATEGORY_NAME'		=> $row['category_name'],
									'CATEGORY_DESCRIPTION'	=> generate_text_for_display($row['category_desc'], $row['category_desc_uid'], $row['category_desc_bitfield'], $row['category_desc_options']),
									'CATEGORY_ITEMS'		=> $row['category_items'],

									'U_CATEGORY'			=> $this->u_action . '&amp;parent_id=' . $row['category_id'],
									'U_MOVE_UP'			=> $url . '&amp;action=move_up',
									'U_MOVE_DOWN'		=> $url . '&amp;action=move_down',
									'U_EDIT'			=> $url . '&amp;action=edit',
									'U_DELETE'			=> $url . '&amp;action=delete',
									'U_SYNC'			=> $url . '&amp;action=sync')
								);
							}
							while ($row = $db->sql_fetchrow($result));
						}
						else if ($parent_id)
						{
							$row = get_category_info($parent_id);
				
							$url = $this->u_action . '&amp;parent_id=' . $parent_id . '&amp;c=' . $row['category_id'];
				
							$template->assign_vars(array(
								'S_NO_CATEGORIES'		=> true,
				
								'U_EDIT'			=> $url . '&amp;action=edit',
								'U_DELETE'			=> $url . '&amp;action=delete',
								'U_SYNC'			=> $url . '&amp;action=sync')
							);
						}
						$db->sql_freeresult($result);

						$template->assign_vars(array(
							'ERROR_MSG'		=> (sizeof($errors)) ? implode('<br />', $errors) : '',
							'NAVIGATION'	=> $navigation,
							'CATEGORY_BOX'		=> $category_box,
							'U_SEL_ACTION'	=> $this->u_action,
							'U_ACTION'		=> $this->u_action . '&amp;parent_id=' . $parent_id,
						));
						
						$template->assign_vars(array(
							'ICON_MOVE_UP'				=> '<img src="' . $phpbb_root_path . 'adm/images/icon_up.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
							'ICON_MOVE_UP_DISABLED'		=> '<img src="' . $phpbb_root_path . 'adm/images/icon_up_disabled.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
							'ICON_MOVE_DOWN'			=> '<img src="' . $phpbb_root_path . 'adm/images/icon_down.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
							'ICON_MOVE_DOWN_DISABLED'	=> '<img src="' . $phpbb_root_path . 'adm/images/icon_down_disabled.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
							'ICON_EDIT'					=> '<img src="' . $phpbb_root_path . 'adm/images/icon_edit.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
							'ICON_EDIT_DISABLED'		=> '<img src="' . $phpbb_root_path . 'adm/images/icon_edit_disabled.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
							'ICON_DELETE'				=> '<img src="' . $phpbb_root_path . 'adm/images/icon_delete.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
							'ICON_DELETE_DISABLED'		=> '<img src="' . $phpbb_root_path . 'adm/images/icon_delete_disabled.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
							'ICON_SYNC'					=> '<img src="' . $phpbb_root_path . 'adm/images/icon_sync.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
							'ICON_SYNC_DISABLED'		=> '<img src="' . $phpbb_root_path . 'adm/images/icon_sync_disabled.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
						));
						
					break;

				}
			
			break;

			case 'profile_info':

				include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
				include($phpbb_root_path . 'includes/functions_display.' . $phpEx);

				$enable_bbcode	= ($config['allow_sig_bbcode']) ? (bool) $user->optionget('sig_bbcode') : false;
				$enable_smilies	= ($config['allow_sig_smilies']) ? (bool) $user->optionget('sig_smilies') : false;
				$enable_urls	= ($config['allow_sig_links']) ? (bool) $user->optionget('sig_links') : false;

				$introduce		= utf8_normalize_nfc(request_var('message', (string) $user->data['introduce'], true));
				$introduce_subject		= utf8_normalize_nfc(request_var('subject', (string) $user->data['introduce_subject'], true));
				$space_name	= utf8_normalize_nfc(request_var('space_name', (string) $user->data['space_name'], true));
				$allow_atom	= ($user->data['allow_atom'] == 1)? true : false;

				add_form_key('ucp_xspace_profile_info');

				if ($submit || $preview)
				{
					include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

					$enable_bbcode	= ($config['allow_sig_bbcode']) ? ((request_var('disable_bbcode', false)) ? false : true) : false;
					$enable_smilies	= ($config['allow_sig_smilies']) ? ((request_var('disable_smilies', false)) ? false : true) : false;
					$enable_urls	= ($config['allow_sig_links']) ? ((request_var('disable_magic_url', false)) ? false : true) : false;
					$allow_atom		= (empty($_POST['allow_atom'])) ? 0 : 1;

					if (!sizeof($error))
					{
						$message_parser = new parse_message($introduce);

						// Allowing Quote BBCode
						$message_parser->parse($enable_bbcode, $enable_urls, $enable_smilies, $config['allow_sig_img'], $config['allow_sig_flash'], true, $config['allow_sig_links'], true, 'sig');

						if (sizeof($message_parser->warn_msg))
						{
							$error[] = implode('<br />', $message_parser->warn_msg);
						}

						if (!check_form_key('ucp_xspace_profile_info'))
						{
							$error[] = 'FORM_INVALID';
						}

						if (!sizeof($error) && $submit)
						{
							$sql_ary = array(
								'space_name'		=> (string)$space_name,
								'introduce'					=> (string) $message_parser->message,
								'introduce_subject'		=> (string) $introduce_subject,
								'introduce_options'				=> $user->data['user_options'],
								'introduce_uid'		=> (string) $message_parser->bbcode_uid,
								'introduce_bitfield'	=> $message_parser->bbcode_bitfield,
								'allow_atom'			=> $allow_atom
							);

							$sql = 'UPDATE ' . XSPACE_CONFIG_TABLE . '
								SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
								WHERE user_id = ' . $user->data['user_id'];
							$db->sql_query($sql);

							meta_refresh(3, $this->u_action);
							$message = $user->lang['XSPACE_UPDATED'] . '<br /><br />' . sprintf($user->lang['RETURN_UCP'], '<a href="' . $this->u_action . '">', '</a>');
							trigger_error($message);
						}
					}

					// Replace "error" strings with their real, localised form
					$error = preg_replace('#^([A-Z_]+)$#e', "(!empty(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '\\1'", $error);
				}

				$introduce_preview = '';
				if ($preview)
				{
					// Now parse it for displaying
					$introduce_preview = $message_parser->format_display($enable_bbcode, $enable_urls, $enable_smilies, false);
					unset($message_parser);
				}

				decode_message($introduce, $user->data['introduce_uid']);

				$template->assign_vars(array(
					'ERROR'				=> (sizeof($error)) ? implode('<br />', $error) : '',
					'INTRODUCE'			=> $introduce,
					'SPACE_NAME'			=> $space_name,
					'SUBJECT'				=> $introduce_subject, 
					'INTRODUCE_PREVIEW'	=> $introduce_preview,

					'S_ALLOW_ATOM_CHECKED'	=> ($allow_atom) ? ' checked="checked"' : '',
					'S_BBCODE_CHECKED' 		=> (!$enable_bbcode) ? ' checked="checked"' : '',
					'S_SMILIES_CHECKED' 	=> (!$enable_smilies) ? ' checked="checked"' : '',
					'S_MAGIC_URL_CHECKED' 	=> (!$enable_urls) ? ' checked="checked"' : '',

					'BBCODE_STATUS'			=> ($config['allow_sig_bbcode']) ? sprintf($user->lang['BBCODE_IS_ON'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>') : sprintf($user->lang['BBCODE_IS_OFF'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>'),
					'SMILIES_STATUS'		=> ($config['allow_sig_smilies']) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
					'IMG_STATUS'			=> ($config['allow_sig_img']) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
					'FLASH_STATUS'			=> ($config['allow_sig_flash']) ? $user->lang['FLASH_IS_ON'] : $user->lang['FLASH_IS_OFF'],
					'URL_STATUS'			=> ($config['allow_sig_links']) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],
					'MAX_FONT_SIZE'			=> (int) $config['max_sig_font_size'],

					'S_BBCODE_ALLOWED'		=> $config['allow_sig_bbcode'],
					'S_SMILIES_ALLOWED'		=> $config['allow_sig_smilies'],
					'S_BBCODE_IMG'			=> ($config['allow_sig_img']) ? true : false,
					'S_BBCODE_FLASH'		=> ($config['allow_sig_flash']) ? true : false,
					'S_LINKS_ALLOWED'		=> ($config['allow_sig_links']) ? true : false)
				);

				// Build custom bbcodes array
				display_custom_bbcodes();

			break;
		}

		$template->assign_vars(array(
			'L_TITLE'	=> $user->lang['UCP_XSPACE_' . strtoupper($mode)],

			'S_HIDDEN_FIELDS'	=> $s_hidden_fields,
			'S_UCP_ACTION'		=> $this->u_action)
		);

		// Set desired template
		$this->tpl_name = 'ucp_xspace_' . $mode;
		$this->page_title = 'UCP_XSPACE_' . strtoupper($mode);
	}
}

?>